﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory
{
	/// <summary>
	/// 工作流模板步骤表单分类
	/// </summary>
	public class WorkflowTemplateStepFormCategoryData : IWorkflowTemplateStepFormCategoryData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatestepformcategory";
		private const string QUERY = @"SELECT WorkflowTemplateStepFormCategoryId,FormCategoryCode,FormCategoryName,CreateAdminId,CreateAdminName,CreateTime,EditAdminId,EditAdminName,UpdateTime FROM oa_workflowtemplatestepformcategory";
		public WorkflowTemplateStepFormCategoryData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板步骤表单分类是否存在
		public async Task<bool> Exists(long workflowTemplateStepFormCategoryId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormCategoryId=?WorkflowTemplateStepFormCategoryId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormCategoryId", workflowTemplateStepFormCategoryId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(string formCategoryCode)
		{
			string sql = string.Format(@"{0} WHERE FormCategoryCode=?FormCategoryCode", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryCode", formCategoryCode, DbType.AnsiString);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板步骤表单分类
		public async Task<WorkflowTemplateStepFormCategoryEntity> Add(WorkflowTemplateStepFormCategoryEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatestepformcategory", new string[] { "FormCategoryCode", "FormCategoryName", "CreateAdminId", "CreateAdminName" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormCategoryId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormCategoryEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流模板步骤表单分类
		public async Task<WorkflowTemplateStepFormCategoryEntity> Update(WorkflowTemplateStepFormCategoryEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatestepformcategory", new string[] { "FormCategoryCode", "FormCategoryName", "EditAdminId", "EditAdminName" }, new string[] { "WorkflowTemplateStepFormCategoryId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormCategoryId=?WorkflowTemplateStepFormCategoryId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormCategoryId", entity.WorkflowTemplateStepFormCategoryId, DbType.Int64, size: 20);
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormCategoryEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板步骤表单分类
		public async Task<bool> Delete(long workflowTemplateStepFormCategoryId)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepformcategory WHERE WorkflowTemplateStepFormCategoryId=?WorkflowTemplateStepFormCategoryId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormCategoryId", workflowTemplateStepFormCategoryId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板步骤表单分类
		public async Task<WorkflowTemplateStepFormCategoryEntity> GetEntity(long workflowTemplateStepFormCategoryId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormCategoryId=?WorkflowTemplateStepFormCategoryId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormCategoryId", workflowTemplateStepFormCategoryId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormCategoryEntity>(sql, parameters);
		}
		public async Task<WorkflowTemplateStepFormCategoryEntity> GetEntity(string formCategoryCode)
		{
			string sql = string.Format(@"{0} WHERE FormCategoryCode=?FormCategoryCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryCode", formCategoryCode, DbType.AnsiString);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormCategoryEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤表单分类列表
		public async Task<IEnumerable<WorkflowTemplateStepFormCategoryEntity>> GetList()
		{
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepFormCategoryEntity>(QUERY);
		}
		#endregion

		#region 获取工作流模板步骤表单分类分页
		public async Task<(IEnumerable<WorkflowTemplateStepFormCategoryEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatestepformcategory {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepFormCategoryEntity, int>(sql, parameters);
		}
		#endregion
	}
}



